/*
 * 文件名：StayCarOutThread.java	 
 * 时     间：下午2:55:12
 * 作     者：lyf      
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.manager.thread;

import java.util.List;

import com.cloud.otherThreads.UploadReportCarInfoRunble;
import com.cloud.threads.UpLoadCarOutInfoRunble;
import com.cz073586.tools.util.Log4jUtil;
import com.cz073586.tools.util.ThreadPoolUtil;
import com.cz073586.tools.util.Tools;
import com.local.dao.mapers.CarParkOutMapper;
import com.local.entity.vo.ParkwellPayBillVo;
import com.local.entity.vo.ReportCarInfoVo;
import com.local.entity.webpush.CarOutPushVo;
import com.local.manager.cfgManager.CloudConfigManager;
import com.local.manager.cfgManager.reportConfigManager;
import com.local.manager.locks.RecordLockManager;
import com.local.manager.push.PushCarOutBillInfoRunble;
import com.local.model.CarParkOut;
import com.local.model.CarParkOutExample;
import com.local.model.Crossing;
import com.local.model.LocalStation;
import com.local.service.face.CarInAndOutService;
import com.local.service.face.CrossingService;
import com.local.service.face.LogService;
import com.qianhai.common.exception.BaseException;

/**
 * 类描述: 相机识别后,处理待出场.标记插入待出场纪录，并上传到云平台，推送前端页面
 * 
 * @ClassName: StayCarOutThread
 * @author lyf
 * @date 2016年3月25日 下午2:55:12
 */
public class StayCarOutThread implements Runnable
{
	private static final Log4jUtil log = Log4jUtil
			.getLog4j(StayCarOutThread.class);
	
	private CarParkOutMapper carParkOutMapper;
	private LogService logService;
	private CarParkOut carParkOut;
	protected CarInAndOutService carInAndOutService;
	private ParkwellPayBillVo parkwellPayBillVo;
	private CrossingService crossinService;
	
	public StayCarOutThread(CarParkOutMapper carParkOutMapper,
			LogService logService, CarParkOut carParkOut,
			CarInAndOutService carInAndOutService,
			ParkwellPayBillVo parkwellPayBillVo, CrossingService crossinService)
	{
		super();
		this.carParkOutMapper = carParkOutMapper;
		this.logService = logService;
		this.carParkOut = carParkOut;
		this.carInAndOutService = carInAndOutService;
		this.parkwellPayBillVo = parkwellPayBillVo;
		this.crossinService = crossinService;
	}
	
	@Override
	public void run()
	{
		
		try
		{
			
			if (log.isDebugEnabled())
			{
				log.debug("-->出场不开闸子线程ID执行:" + Thread.currentThread().getId()
						+ "," + carParkOut.getRecordId() + "车牌号"
						+ carParkOut.getCarNo());
			}
			
			if (!carParkOut.getCarNo().equals("")
					&& !"无效车牌".equalsIgnoreCase(carParkOut.getCarNo()))
			{
				// TODO lyf 删除重复出场信息
				CarParkOutExample condition = new CarParkOutExample();
				condition.or().andCarNoEqualTo(carParkOut.getCarNo())
						.andStatusEqualTo(1);
				carParkOutMapper.deleteByCondition(condition);
			}
			
			
			//添加出口岗亭信息 2016-8-10
			List<Crossing> crossList = crossinService
					.queryCrossingByEquimentNo(carParkOut.getEquipment());
			
			if(!crossList.isEmpty())
			{
				LocalStation staion = crossinService.findStationByCrosId(crossList.get(0).getUuid());
				if(!Tools.isEmpty(staion))
				{
					carParkOut.setStationUuid(staion.getStationUuid());
				}
			}
			//添加出口岗亭信息 2016-8-10
			
			/************ the end ************/
			carParkOutMapper.insertSelective(carParkOut);
			// 解锁
			RecordLockManager.instance().removeLock(carParkOut.getRecordId());
			if (log.isDebugEnabled())
			{
				log.debug("--->解开锁(弹框),出场不开闸子线程ID"
						+ Thread.currentThread().getId() + "，纪录ID"
						+ carParkOut.getRecordId() + "，车牌号:"
						+ carParkOut.getCarNo());
			}
			// 推送到前端页面，弹出框
			CarOutPushVo carOutPushVo = new CarOutPushVo();
			carOutPushVo.setCarNo(carParkOut.getCarNo());
			carOutPushVo.setCarNoTye(parkwellPayBillVo.getCarNoTye());
			carOutPushVo.setCarOutUuid(carParkOut.getUuid());
			carOutPushVo.setEuqmentNo(carParkOut.getEquipment());
			carOutPushVo.setIsOpenGate("2");
			// end 出场弹框
			PushCarOutBillInfoRunble pushToWeb = new PushCarOutBillInfoRunble(
					crossinService, carOutPushVo);
			ThreadPoolUtil.instance().execute(pushToWeb);
			
			// 出场记录上传到云
			if (CloudConfigManager.instance().isOpen())
			{
				UpLoadCarOutInfoRunble upload = new UpLoadCarOutInfoRunble(
						carParkOut, logService);
				ThreadPoolUtil.instance().execute(upload);
			}
			
			// 上报车辆出场，信息
			if (reportConfigManager.instance().isOpen())
			{
				// 上报系统，转换
				ReportCarInfoVo pr = carInAndOutService
						.ReportCarInfoVoOut(carParkOut);
				
				UploadReportCarInfoRunble upload = new UploadReportCarInfoRunble(
						pr, logService);
				ThreadPoolUtil.instance().execute(upload);
			}
		} catch (BaseException e)
		{
			log.error("StayCarOutThread", e);
			
			if (log.isDebugEnabled())
			{
				log.debug("--->解锁不开闸弹框异常(出),出场不开闸子线程ID"
						+ Thread.currentThread().getId() + ",出场纪录ID"
						+ carParkOut.getRecordId() + "，车牌号:"
						+ carParkOut.getCarNo());
			}
			RecordLockManager.instance().removeLock(carParkOut.getRecordId());
		}
	}
	
}
